//var xsjsdDsyfje = 0;
var bzjfs = 0;
$(function () {
    //申报公司对申报部门申报人的级联操作
    $("#xsjsd_gs").czgsjgqczcombobox({
        onSuccess: function () {
            $("#xsjsd_gs").zcombobox("setMap", { id: $("#xsjsd_content_gs_id").val(), qc: $("#xsjsd_content_gs_jc").val()});
        },
        onSelect: function () {
            var id_val = $('#xsjsd_gs').val();
            $("#xsjsd_content_glqy").combobox('reload', $.webPath() + '/xsgl/xsdgl/xsdKhList?xsgsId=' + id_val + '&sbgsId=' + id_val)
        }
    });

//    $("#xsjsd_xsgs").gsjgtree(1, {
//        onLoadSuccess:function () {
//            $("#xsjsd_xsgs").combotree('readonly', true);
//        }
//    })
    //客户的加载
//    $("#xsjsd_content_glqy").qyjgzcombobox({
//        onSuccess:function () {
//            $("#xsjsd_content_glqy").zcombobox("setMap", { id:$("#xsjsd_content_kh_id").val(), jc:$("#xsjsd_content_kh_jc").val()});
//        },
//        onSelect:function () {
//            var id_val = $('#xsjsd_content_glqy').val();
//            $.post($.webPath() + '/xsgl/xsdgl/xsdKhById', {'xsgsId':id_val}, function (data) {
//                $('#xsjsd_glqy_qyfr_nsh').val($.trim(data[0].nsh));
//            })
//        }
//    });

//    $("#xsjsd_zbjs").unbind('click').bind('click', function () {
//        if ($(this).attr("checked") != undefined && $(this).attr("checked") == "checked") {
//            //TO-DO 赋值公司本部
//            $("#xsjsd_xsgs").combotree('setValue', 1);
//            $("#xsjsd_content_glqy").combobox('clear');
//            $("#xsjsd_content_glqy").combobox('reload', $.webPath() + '/xsgl/xsdgl/xsdKhList?xsgsId=1&sbgsId=' + $("#xsjsd_gs").val());
//        } else {
//            $("#xsjsd_xsgs").combotree('setValue', $("#xsjsd_gs").val());
//            $("#xsjsd_content_glqy").combobox('clear');
//            $("#xsjsd_content_glqy").combobox('reload', $.webPath() + '/xsgl/xsdgl/xsdKhList?xsgsId=' + $("#xsjsd_gs").val() + '&sbgsId=' + $("#xsjsd_gs").val());
//        }
//    });

    if ($("#xsjsd_id").val()) {
        $("#xsjsd_zbjs").unbind('click').bind('click', function () {
            return false;
        });
    }

    var xsjsdId = $("#xsjsd_id").val() ? $("#xsjsd_id").val() : 0;

    $("#grid_xsd").datagrid({
        url: $.webPath() + "/xsgl/xsdgl/xsdmxSearchList?xsjsdId=" + xsjsdId || 0,
        width: 1050,
        height: 275,
        fitColumns: false,
        fit: false,
        singleSelect: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: true,
        rownumbers: true,
        frozenColumns: [
            [
                {field: 'checked', checkbox: true},
                {field: 'xxsdh', title: '销售合同子项号', width: 120},
                {field: 'xsdh', title: '销售单号', width: 120},
                {field: 'wzgg.name', title: '物资名称', width: 70},
                {field: 'wzgg.cz', title: '材质', width: 70},
                {field: 'wzgg.ggxh', title: '规格型号', width: 70},
                {field: 'carNo', title: '车皮号', width: 70},
                {field: 'bindNo', title: '捆包号', width: 70},
                {field: 'wzgg.dw', title: '单位', width: 50}
            ]
        ],
        columns: [
            [
                {field: 'hsj', title: '合同含税价', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120},
                {field: 'sjdj', title: '实际单价', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120},
                {field: 'cksl', title: '数量', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 100},
                {field: 'sjghje', title: '购货金额（含税）', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120},
                {field: 'txx', title: '贴现息', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 90},
                {field: 'dlf', title: '代理费', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 90},
                /* {field: 'zjzyf', title: '资金占用费', formatter: function (value) {
                 var sl = formatJe(value, ",");
                 return sl;
                 }, width: 120},
                 {field: 'xkyh', title: '现款优惠', formatter: function (value) {
                 var sl = formatJe(value, ",");
                 return sl;
                 }, width: 90},*/
                {field: 'se', title: '赎货金额', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120},
                {field: 'kpbhsj', title: '开票不含税单价', formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 120},
                {field: 'je', title: '金额', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120},
                {field: 'sje', title: '税额', formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }, width: 120}
            ]
        ]
    })

    //发票总额的变换填充
//    $('#xsjsd_ddfy').focusout(function () {
//        var ddyf = parseFloat($('#xsjsd_ddfy').val());
//        var me = parseFloat($('#xsjsd_me').val());
//        if (ddyf) {
//            $('#xsjsd_fpze').val(me + ddyf);
//        }
//    });


    // 新增销售单明细
    $("#btn_xsd_search").click(function () {
        var xsjsd_gs = $("#xsjsd_sbgs_id").val();
        var xsjsd_glqy = $("#xsjsd_content_glqy").combobox('getValue');
        if (xsjsd_glqy) {
            searcxsdhwindow(grid_xsd_searchWindowOKbtnHandler, xsjsd_gs, xsjsd_gs, xsjsd_glqy);
        } else {
            $('#xsjsd_tabs').tabs('select', "销售单信息");
            $.messager.alert("提示", "请选择客户!", "info");
        }
    });

    //删除销售单的按钮触发
    $("#btn_xsd_del").click(function () {
        var xsdRow = $('#grid_xsd').datagrid('getChecked');
        if (xsdRow) {
            $.messager.confirm('提示', '是否确定删除销售明细?', function (r) {
                if (r) {
                    $.each(xsdRow, function (i, obj) {
                        var index = $("#grid_xsd").edatagrid('getRowIndex', obj);
                        $("#grid_xsd").edatagrid('deleteRow', index);
                    });
                    countDomData();
                    $("#btn_xsjsd_txx").attr("checked", false);
                    $("#btn_xsjsd_dlf").attr("checked", false);
                }
            });
        } else {
            $.messager.alert('提示', '请选择要删除的销售单!');
        }
    });

    //客户关联纳税号
    $('#xsjsd_content_glqy').combobox({
        onSelect: function () {
            var obj = $('#xsjsd_content_glqy').combobox('getData');
            var id_val = $('#xsjsd_content_glqy').combobox('getValue');
            $.each(obj, function (i, val) {
                if (obj[i].id == id_val) {
                    $('#xsjsd_glqy_qyfr_nsh').val(obj[i].qyfr_nsh);
//                    bzjfs = obj[i].bzjfs;
//                    if (bzjfs == 0) {
//                        $("#sfjsdlf_id").show();
//                        $("#sfjstx_id").hide();
//                    } else {
//                        $("#sfjstx_id").show();
//                        $("#sfjsdlf_id").hide();
//                    }
                }
            })
            var com_data = $('#grid_xsd').edatagrid("getData");
            if (com_data && com_data.rows.length > 0) {
                $.each(com_data.rows, function (i, obj) {
                    var index = $("#grid_xsd").edatagrid('getRowIndex', obj);
                    $("#grid_xsd").edatagrid('deleteRow', index);
                });
            }

        }
    });

    //计算贴息
    var txx;
    $("#btn_xsjsd_txx").click(function () {
        var TxxButton = document.getElementById("btn_xsjsd_txx");
        if (TxxButton.checked) {
            //获取所有行 把贴息平分
            var Now_data = $('#grid_xsd').edatagrid("getData");
            var xsmxIds = "";
            for (var i = 0; i < Now_data.rows.length; i++) {
                xsmxIds += Now_data.rows[0].id + ",";
            }
            if (xsmxIds && Now_data.rows.length) {
                var xsjsd_glqy = $("#xsjsd_content_glqy").combobox('getValue');
                $.post($.webPath() + '/jsgl/xsjsd/xsjsd_txx', {'xsmxIds': xsmxIds}, function (data) {
                    txx = parseFloat(data);
                    $('#xsjsd_txje').val(formatJe2(txx));
                    var fpze = 0;
                    var zcksl = 0;
                    var xsjsdJe = 0;
                    var xsjsdse = 0;
                    // 总重量
                    for (var i = 0; i < Now_data.rows.length; i++) {
                        zcksl += parseFloat(Now_data.rows[i]['cksl']);
                    }
                    for (var i = 0; i < Now_data.rows.length; i++) {
                        // 实际分配贴息
                        var sjtx = (parseFloat(Now_data.rows[i]['cksl']) / zcksl) * txx;
                        var se = parseFloat(Now_data.rows[i]['sjghje']) + sjtx + parseFloat(Now_data.rows[i]['dlf']) + parseFloat(Now_data.rows[i]['zjzyf']) + parseFloat(Now_data.rows[i]['xkyh']);
                        var kpbhsj = Math.round((se / (1.17 * parseFloat(Now_data.rows[i]['cksl']))) * 1000000) / 1000000;
                        var je = parseFloat(kpbhsj * parseFloat(Now_data.rows[i]['cksl']));
                        var kpse = parseFloat(parseFloat(se) - parseFloat(je));
                        $('#grid_xsd').datagrid('updateRow', {
                            index: i,
                            row: {
                                txx: parseFloat(sjtx),
                                se: se,
                                kpbhsj: kpbhsj,
                                je: je,
                                sje: kpse
                            }
                        });
                        fpze += parseFloat(Now_data.rows[i]['sjghje']) + parseFloat(sjtx) + parseFloat(Now_data.rows[i]['dlf']) + parseFloat(Now_data.rows[i]['zjzyf']) + parseFloat(Now_data.rows[i]['xkyh']);
                        xsjsdJe += parseFloat(Now_data.rows[i]['je']);
                        xsjsdse += parseFloat(Now_data.rows[i]['sje']);
                    }
                    //  $('#xsjsd_me').val(formatJe2(fpze));
                    $('#xsjsd_fpze').val(formatJe2(fpze));
                    $('#xsjsd_je').val(formatJe2(xsjsdJe));
                    $('#xsjsd_se').val(formatJe2(xsjsdse));
                })
            }

        } else {
            var Now_data = $('#grid_xsd').edatagrid("getData");
            $('#xsjsd_txje').val(formatJe2(0.0));
            if (Now_data.rows.length) {
                var fpze = 0;
                var xsjsdJe = 0;
                var xsjsdse = 0;
                for (var i = 0; i < Now_data.rows.length; i++) {
                    // 实际分配贴息
                    var sjtx = 0;
                    var se = parseFloat(Now_data.rows[i]['sjghje']) + sjtx + parseFloat(Now_data.rows[i]['dlf']) + parseFloat(Now_data.rows[i]['zjzyf']) + parseFloat(Now_data.rows[i]['xkyh']);
                    var kpbhsj = Math.round((se / (1.17 * parseFloat(Now_data.rows[i]['cksl']))) * 1000000) / 1000000;
                    var je = parseFloat(kpbhsj * parseFloat(Now_data.rows[i]['cksl']));

                    $('#grid_xsd').datagrid('updateRow', {
                        index: i,
                        row: {
                            txx: 0,
                            se: se,
                            kpbhsj: kpbhsj,
                            je: je,
                            sje: se - je
                        }
                    });
                    fpze += parseFloat(Now_data.rows[i]['sjghje']) + 0 + parseFloat(Now_data.rows[i]['dlf']) + parseFloat(Now_data.rows[i]['zjzyf']) + parseFloat(Now_data.rows[i]['xkyh']);
                    xsjsdJe += Now_data.rows[i]['je'];
                    xsjsdse += Now_data.rows[i]['sje'];
                }
                //  $('#xsjsd_me').val(formatJe2(fpze));
                $('#xsjsd_fpze').val(formatJe2(fpze));
                $('#xsjsd_je').val(formatJe2(xsjsdJe));
                $('#xsjsd_se').val(formatJe2(xsjsdse));
            }
        }
    })


    //计算代理费用(包括贴息、代理费、资金占用费等)
    var ztx;
    var zdlf;
    var zzjzyf;
    $("#btn_xsjsd_dlf").click(function () {
        var DlfButton = document.getElementById("btn_xsjsd_dlf");
        if (DlfButton.checked) {
            //获取所有行 把贴息平分
            var Now_data = $('#grid_xsd').edatagrid("getData");
//            $('#xsjsd_txje').val(formatJe2(0.0));

//            $('#xsjsd_zjzyfje').val(formatJe2(0.0));
            if (Now_data.rows.length) {
                var xsjsd_glqy = $("#xsjsd_content_glqy").combobox('getValue');
                $.post($.webPath() + '/jsgl/xsjsd/xsjsd_dlfy', {'xsmxId': Now_data.rows[0].id}, function (data) {
                    ztx = parseFloat(data.ztx);
                    zdlf = parseFloat(data.zdlf);
                    zzjzyf = parseFloat(data.zzjzyf);
                    var fpze = 0;
                    var zcksl = 0;
                    var xsjsdJe = 0;
                    var xsjsdse = 0;
                    // 总重量
                    for (var i = 0; i < Now_data.rows.length; i++) {
                        zcksl += parseFloat(Now_data.rows[i]['cksl']);
                    }
                    for (var i = 0; i < Now_data.rows.length; i++) {
                        // 实际分配贴息
                        var sjtx = parseFloat(Now_data.rows[i]['txx']);
                        var sdlf = (parseFloat(Now_data.rows[i]['cksl']) / zcksl) * zdlf;
                        var szjzyf = (parseFloat(Now_data.rows[i]['cksl']) / zcksl) * zzjzyf;
                        var sse = parseFloat(Now_data.rows[i]['sjghje']) + sjtx + sdlf + szjzyf + parseFloat(Now_data.rows[i]['xkyh']);
                        var kpbhsj = Math.round((sse / (1.17 * parseFloat(Now_data.rows[i]['cksl']))) * 1000000) / 1000000;
                        var je = parseFloat(kpbhsj * parseFloat(Now_data.rows[i]['cksl']));
                        $('#grid_xsd').datagrid('updateRow', {
                            index: i,
                            row: {
                                txx: parseFloat(sjtx),
                                dlf: parseFloat(sdlf),
                                zjzyf: parseFloat(szjzyf),
                                se: sse,
                                kpbhsj: kpbhsj,
                                je: je,
                                sje: sse - je
                            }
                        });
                        fpze += sse;
                        xsjsdJe += Now_data.rows[i]['je'];
                        xsjsdse += Now_data.rows[i]['sje'];
                    }
                    //   $('#xsjsd_me').val(formatJe2(fpze));
                    $('#xsjsd_fpze').val(formatJe2(fpze));
                    $('#xsjsd_je').val(formatJe2(xsjsdJe));
                    $('#xsjsd_se').val(formatJe2(xsjsdse));
//                    $('#xsjsd_dlfje').val(formatJe2(zdlf));
                })

            }
        } else {
            var Now_data = $('#grid_xsd').edatagrid("getData");
//            $('#xsjsd_txje').val(formatJe2(ztx));
//            $('#xsjsd_zjzyfje').val(formatJe2(zzjzyf));
            if (Now_data.rows.length) {
                var fpze = 0;
                var xsjsdJe = 0;
                var xsjsdse = 0;
                for (var i = 0; i < Now_data.rows.length; i++) {
                    var sjtx = 0;
                    var sdlf = 0;
                    var szjzyf = 0;
                    var sse = parseFloat(Now_data.rows[i]['sjghje']) + sjtx + sdlf + szjzyf + parseFloat(Now_data.rows[i]['xkyh']);
                    var kpbhsj = Math.round((sse / (1.17 * parseFloat(Now_data.rows[i]['cksl']))) * 1000000) / 1000000;
                    var je = parseFloat(kpbhsj * parseFloat(Now_data.rows[i]['cksl']));
                    $('#grid_xsd').datagrid('updateRow', {
                        index: i,
                        row: {
                            txx: parseFloat(sjtx),
                            dlf: parseFloat(sdlf),
                            zjzyf: parseFloat(szjzyf),
                            se: sse,
                            kpbhsj: kpbhsj,
                            je: je,
                            sje: sse - je
                        }
                    });
                    fpze += parseFloat(Now_data.rows[i]['sjghje']) + sjtx + sdlf + szjzyf + parseFloat(Now_data.rows[i]['xkyh']);
                    xsjsdJe += Now_data.rows[i]['je'];
                    xsjsdse += Now_data.rows[i]['sje'];
                }
                //  $('#xsjsd_me').val(formatJe2(fpze));
                $('#xsjsd_fpze').val(formatJe2(fpze));
                $('#xsjsd_je').val(formatJe2(xsjsdJe));
                $('#xsjsd_se').val(formatJe2(xsjsdse));
//                $('#xsjsd_dlfje').val(formatJe2(sdlf));
            }
        }
    })
});

//查询销售单确定按钮回调函数
function grid_xsd_searchWindowOKbtnHandler(data) {
    var xsjsdData = $("#grid_xsd").datagrid("getRows");
    var xsdIds = "";
    $.each(data, function () {
        xsdIds += this.id + ",";
    });
    if (xsdIds) {
        $.post($.webPath() + '/jsgl/xsjsd/xsjsd_txdlf', {'xsdIds': xsdIds}, function (rdata) {
            var ztx = parseFloat(rdata.ztx);
            var zdlf = parseFloat(rdata.zdlf);
            var zzjzyf = parseFloat(rdata.zzjzyf);
            $('#xsjsd_txje').val(formatJe2(ztx));
            $('#xsjsd_dlfje').val(formatJe2(zdlf));
            $('#xsjsd_zjzyfje').val(formatJe2(zzjzyf));
        })
    }
    $.each(data, function () {
        var xsdId = this.id;
        var xsdh = this.xsdh;
        var map = new Map();
        $.post($.webPath() + '/xsgl/xsdgl/xsdgl_js_xsmx', {'xsdId': xsdId}, function (mxData) {
            //xsjsdDsyfje = !$("#xsjsd_ddfy").val() ? 0 : $("#xsjsd_ddfy").val();
            $.each(mxData.rows, function (i, j) {
                var flag = false;
                $.each(xsjsdData, function (r, obj) {
                    if (xsjsdData.length != 0 && obj['id'] == j['id']) {
                        flag = true;
                        return false;
                    }
                })
                if (!flag) {
                    if (!map.get(this['xsd.id']) && this['dsyfje'])
                        map.put(this['xsd.id'], this['dsyfje']);
                    var hsj = Math.round(this['hsj'] * 10000) / 10000;
                    var sl = Math.round(this['cksl'] * 1000000) / 1000000;
                    var sjdj = Math.round(this['sjdj'] * 10000) / 10000;
                    var txx = 0;
                    var dlf;
                    if (bzjfs == 1) {
                        dlf = Math.round(this['jfmxdlf'] * 10000) / 10000;
                    } else {
                        dlf = 0;
                    }
                    var zjzyf = 0;
                    var xkyh = 0;
                    // 赎货金额
                    var se = (sjdj * sl) + txx + dlf + zjzyf + xkyh;
                    var kpbhsj = se / (1.17 * sl);
                    // 开票金额
                    var je = kpbhsj * sl;
                    // 税额
                    var sje = se - je;
                    $("#grid_xsd").edatagrid('appendRow', {
                        'id': this.id,
                        'xsd.id': xsdId,
                        'xsd.sl': this['xsd.sl'],
                        'xsdh': xsdh,
                        'xxsdh': this['xxsdh'],
                        'wzgg.code': this['wzgg.code'],
                        'wzgg.name': this['wzgg.name'],
                        'wzgg.cz': this['wzgg.cz'],
                        'wzgg.ggxh': this['wzgg.ggxh'],
                        'carNo': this['carNo'],
                        'bindNo': this['bindNo'],
                        'wzgg.dw': this['wzgg.dw'],
                        'sjdj': sjdj,
                        'sjghje': sjdj * sl,
                        'cksl': sl,
                        'se': se,
                        'kpbhsj': kpbhsj,
                        'hsj': hsj,
                        'txx': txx,
                        'dlf': dlf,
                        'zjzyf': zjzyf,
                        'xkyh': xkyh,
                        'je': je,
                        'sje': sje
                    });
                }
            });

//            for (var i = 0; i < map.size(); i++) {
//                xsjsdDsyfje += map.element(i).value;
//            }
            countDomData();
//            $('#xsjsd_ddfy').val(formatJe2(xsjsdDsyfje));
        })
    });
}


function countDomData() {
    var com_data = $('#grid_xsd').edatagrid("getData");
    var je = 0;
    var se = 0;
    var sje = 0;
    if (com_data.rows.length > 0) {
        for (var i = 0; i < com_data.rows.length; i++) {
            $('#grid_xsd').datagrid('updateRow', {
                index: i,
                row: {
                    txx: parseFloat(com_data.rows[i]['txx']),
                    dlf: parseFloat(com_data.rows[i]['dlf']),
                    zjzyf: parseFloat(com_data.rows[i]['zjzyf']),
                    xkyh: 0,
                    se: parseFloat(com_data.rows[i]['se'])
                }
            });
            je += parseFloat(com_data.rows[i]['je']);
            se += parseFloat(com_data.rows[i]['se']);
            sje += parseFloat(com_data.rows[i]['sje']);
        }
    }
    //  $('#xsjsd_me').val(formatJe2(se));
    $('#xsjsd_je').val(formatJe2(je));
    $('#xsjsd_se').val(formatJe2(sje));
    $('#xsjsd_fpze').val(formatJe2(se));
}
